<?php
/**
 * user获取用户信息接口调用
 * @文件名称: GetInfoAction.php
 * @author: jawei
 * @Email: gaozhiwei@etcp.cn
 * @Date: 2017-06-06
 * @Copyright: 2017 悦畅科技有限公司. All rights reserved.
 * 注意：本内容仅限于悦畅科技有限公司内部传阅，禁止外泄以及用于其他的商业目的
 */
namespace app\modules\user\actions\user;

use app\base\BaseService;
use app\lib\Common;
use app\modules\user\services\user\UserService;
use yii\base\Action;
use yii;

class LoginAction extends Action {
    public function run() {
        $username = trim(Yii::$app->request->post('user_name', null));
        $password = Yii::$app->request->post('password', null);
        $session_id = Yii::$app->request->post('session_id', null);
        $verify_code = ucwords(Yii::$app->request->post('verify_code', null));
        $session = Yii::$app->session;
        $sessionVal = $session->get('rangCode');
        if($sessionVal != $session_id && !empty($session_id)) {
            return BaseService::returnErrData([], 500, '无效的请求');
        }
        if(empty($verify_code)) {
            return BaseService::returnErrData([], 500, '验证码不能为空');
        }
        $session = Yii::$app->session;
        $imgCode = $session->get('imgCode');
        if($verify_code != $imgCode) {
            return BaseService::returnErrData([], 50045, '验证码输入有误');
        } else {
            $session->set('imgCode' , '');
        }
        $isPhone = Common::pregPhonNum($username);
        if(!$isPhone) {
            return BaseService::returnErrData([], 500, '账号格式有误');
        }
        if($username && $password && $session_id) {
            $ret = UserService::UserLogin($username, $password);
            if(BaseService::checkRetIsOk($ret)) {
                $session->remove('rangCode');
            }
            return $ret;
        }
        return BaseService::returnErrData([], 500, '请求参数异常');
    }
}